package com.ait.edu.service.impl;

import com.ait.commonutils.exceptionHandler.GuilException;
import com.ait.edu.entity.Chapter;
import com.ait.edu.entity.Course;
import com.ait.edu.entity.CourseDescription;
import com.ait.edu.entity.Teacher;
import com.ait.edu.entity.vo.CourseFrontVo;
import com.ait.edu.entity.vo.CourseInfoVo;
import com.ait.edu.entity.vo.CoursePublishVo;
import com.ait.edu.entity.vo.CourseWebVo;
import com.ait.edu.mapper.CourseMapper;
import com.ait.edu.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-14
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseDescriptionService courseDescriptionService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private TeacherService teacherService;

    @Override

    //把courseInfoVo 分装成course和courseDescription这个两个对象 然后添加数据库
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //像课程表添加课程的基本信息将courseInfoVo转换成Course
        Course course = new Course();
        //Course 里面的id是随机出来的所以可以取出
        //BeanUtils.copyProperties可以根据你的属性名来进行转换
        BeanUtils.copyProperties(courseInfoVo,course);
        //System.out.println(this.save(course));

        if(!this.save(course)){
            throw new GuilException(500,"添加失败");
        }

        String pid = course.getId();


        //像课程表添加课程的基本信息将courseInfoVo转换成courseDescription
        CourseDescription courseDescription = new CourseDescription();
        
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(pid);
        if(!courseDescriptionService.save(courseDescription)){
            throw new GuilException(500,"添加失败");
        }

        return pid;
    }

    //获取到数据Course和CourseDescription然后封装成CourseInfoVo返回出去
    @Override
    public CourseInfoVo getCourseInfoVoId(String id) {
        Course course = this.getById(id);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(course,courseInfoVo);
        CourseDescription courseDescription = courseDescriptionService.getById(id);
        if(courseDescription!=null){
            BeanUtils.copyProperties(courseDescription,courseInfoVo);
        }

        return courseInfoVo;
    }

    //修改数据跟获取数据一样
    @Override
    public String modifyCourseInfoVo(CourseInfoVo courseInfoVo) {
        //像课程表添加课程的基本信息将courseInfoVo转换成Course
        Course course = new Course();
        //BeanUtils.copyProperties可以根据你的属性名来进行转换
        BeanUtils.copyProperties(courseInfoVo,course);
        if(!this.updateById(course)){
            throw new GuilException(500,"修改失败");
        }

        CourseDescription courseDescription = new CourseDescription();

        BeanUtils.copyProperties(courseInfoVo,courseDescription);

        if(!courseDescriptionService.updateById(courseDescription)){
            throw new GuilException(500,"修改失败");
        }

        return courseDescription.getId();

    }



    //获取链表数据
    @Override
    public CoursePublishVo getCoursePublishV(String id) {
       return   baseMapper.getCoursePublishV(id);
    }


    /*//删除课程 是先删除小节在删除章节在删除描述在删除章节
    *
    * 正确返回true否则返回false
    *
    * */
    @Override
    public boolean removeCourse(String id) {

        //根据id删除小节
        videoService.removeVideo(id);



        //根据id删除章节
        QueryWrapper<Chapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",id);
        chapterService.remove(chapterQueryWrapper);
        //根据id删除描述
        courseDescriptionService.removeById(id);

        //根据id删除课程
        return this.removeById(id);
    }


    //获取首页数据的课程和讲师
    @Cacheable(key = "'TeacherCouresAll'",value = "TeacherCoures")
    @Override
    public Map<String, Object> getTeacherCouresAll() {
        QueryWrapper<Course> wrapperCourse = new QueryWrapper<>();
        wrapperCourse.orderByDesc("id");
        wrapperCourse.last("limit 8");

        List<Course> courses = this.list(wrapperCourse);


        QueryWrapper<Teacher> wrapperTeacher = new QueryWrapper<>();
        wrapperTeacher.orderByDesc("id");
        wrapperTeacher.last("limit 4");

        List<Teacher> teacherList = teacherService.list(wrapperTeacher);
        Map<String, Object> map = new HashMap<>();
        if (courses.size()>0) {

            map.put("Course",courses);

        }
        if (teacherList.size()>0) {

            map.put("teacher",teacherList);

        }
        return map;
    }



    /*
    * 前台显示
    *
    * 课程列表
    *
    * 返回map
    *
    *
    * */
    @Override
    public Map<String, Object> getpageCourselist(Page<Course> page, CourseFrontVo courseFrontVo) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();

        if(courseFrontVo!=null){
            if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){//一级分类 有一级才有二级
                wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
                if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())){//二级分类
                    wrapper.eq("subject_id",courseFrontVo.getSubjectId());
                }
            }
            if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) {//关注度
                wrapper.orderByDesc("buy_count");
            }
            if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) {//时间
                wrapper.orderByDesc("gmt_create");
            }
            if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {//价格
                wrapper.orderByDesc("price");
            }


        }
        this.page(page, wrapper);//查询

        HashMap<String, Object> map = new HashMap<>();

        List<Course> records = page.getRecords();
        long total = page.getTotal();
        long current = page.getCurrent();
        long size = page.getSize();
        long pages = page.getPages();

        boolean hasNext = page.hasNext();
        boolean hasPrevious = page.hasPrevious();


        map.put("items",records);
        map.put("total",total);
        map.put("current",current);
        map.put("size",size);
        map.put("pages",pages);

        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return map;

    }


    @Override
    public CourseWebVo getBaseCourseInfo(String id) {


        return baseMapper.getBaseCourseInfo(id);
    }
}
